package com.shu.Algorithm;

import com.google.common.collect.Lists;

import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

/**
 * @author ssm
 * @version V1.0
 * @description:
 * @date 2021/8/4 14:55
 */

public class BubbleSort {

    {


    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        BubbleSort sort = new BubbleSort();
//        sort.Bubble();
//        sort.BinarySearch();

        sort.maopao();
    }

    /**
     * 冒泡算法
     * *
     */
    public void Bubble() {
        int[] as = {1, 3, 4, 5, 9, 111, 23, 90, 2, 111, 87};
        int temp = 0;
        for (int i = 1; i < as.length; i++) {
            boolean changeFlag = false;
            for (int j = 0; j < as.length - i; j++) {
                if (as[j] > as[j + 1]) {
                    temp = as[j + 1];
                    as[j + 1] = as[j];
                    as[j] = temp;
                    changeFlag = true;
                }
            }
            if (changeFlag) {
                return;
            }
        }
        Arrays.stream(as).forEach(System.out::println);
    }

    /**
     * 二分算法
     */
    public void BinarySearch() {
        IntStream intStream = IntStream.range(0, 100);
        int[] ints = intStream.toArray();
        // Arrays.stream(as).forEach(System.out::println);
        int index = doBinarySearch(ints, 0, ints.length - 1, 75);
        System.out.println("位置是" + index);
    }

    private int doBinarySearch(int[] ints, int start, int end, int targetVal) {
        int mid = (start + end) / 2;
        if (targetVal > ints[mid]) {
            return doBinarySearch(ints, mid + 1, end, targetVal);
        } else if (targetVal == ints[mid]) {
            return mid;
        } else if (targetVal < ints[mid]) {
            return doBinarySearch(ints, start, mid - 1, targetVal);
        } else {
            return 1;
        }
    }


    /**
     * 选择排序算法
     */
    private void select() {


    }


    /**
     * 插入排序算法
     */
    private void insert() {


    }

    /**
     * 冒泡排序算法
     */
    private void maopao() {
        List<Integer> list = Lists.newArrayList(1, 111, 123, 90, 789, 123, 2324);

        // 冒泡排序算法
        int size = list.size();
        for (int i = 0; i < size - 1; i++) {
            for (int j = 0; j < size - i - 1; j++) {
                if (list.get(j) > list.get(j + 1)) {
                    Integer temp = list.get(j);
                    list.set(j, list.get(j + 1));
                    list.set(j + 1, temp);
                }
            }
        }

        list.stream().forEach(System.out::println);
    }

}